/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.pdx.cs.guavalib;

import edu.pdx.cs.guavadata.GSeed;
import edu.pdx.cs.guavadata.Guava;
import edu.pdx.cs.guavadata.domain.Domain;
import edu.pdx.cs.guavadata.subclass.ControlType;
import edu.pdx.cs.guavadata.subclass.GuavaForm;
import edu.pdx.cs.guavadata.subclass.GuavaLaunchBase;
import edu.pdx.cs.guavadata.subclass.LaunchType;
import java.awt.Component;
import java.awt.Dialog.ModalExclusionType;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.beans.*;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author VuTran
 */
public class GJButton extends JButton implements Guava, GuavaLaunchBase {

    String gname;
    String strLaunchFile;
    Class s;

    public GJButton() {
        super();
        this.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActionPerformed(evt);
            }
        });
    }

    /**
     * @return the name
     */
    public String getGLaunchType() {
        return strLaunchFile;
    }

    public void setGLaunchType(String launchType) {
        this.strLaunchFile = launchType;
    }

    public Class getType() {
        return s;
    }

    public void setType(Class s) {
        this.s = s;
    }

    public Class getLaunchType() {
        if ((s == null) && strLaunchFile.length() > 0) {
            try {
                s = Class.forName(strLaunchFile);
            } catch (ClassNotFoundException ex) {
                System.err.println("ERROR: " + ex.toString());
            }
        }
        return s;
    }

    public LaunchType getLType() {
        return LaunchType.Single;
    }

    public ControlType getCType() {
        return ControlType.None;
    }

    public String getGText() {
        return this.getText();
    }

    public String getGName() {
        if (gname == null)
            return this.getName();
        return gname;
    }

    public void setGName(String GName) {
        this.gname = GName;
    }

    public Point getGLocation() {
        return this.getLocation();
    }

    public Dimension getGSize() {
        return this.getSize();
    }

    public List<Guava> getGComponents() {
        try {
            List<Guava> lg = new ArrayList<Guava>();
            if (getLaunchType() == null) {
                return lg;
            }
            Constructor ci = s.getConstructor(new Class[]{});
            Object o = ci.newInstance(new Object[] { });
            lg.add((Guava) o);

            return lg;
        } catch (Exception ex) {
            System.err.println("ERROR: " + ex.toString());
        }
        return null;
    }

    public void gBind(String table, int i) {
        return;
    }

    public void gBindFromDatabase(String table, int i) {
        return;
    }
    
    public String getGDefault() {
        return null;
    }

    public String getGToolTip() {
        return this.getToolTipText();
    }

    public Domain getGDomain() {
        return null;
    }

    public Guava get(String s) {
        for (Guava g : this.getGComponents()) {
            if (g.getGName().equals(s)) {
                return g;
            }
        }
        return null;
    }

    public void setObject(UUID id) {
        return;
    }

    private void btnActionPerformed(ActionEvent evt) {
        // get parent form
        Component p = this.getTopLevelAncestor();
        if (!(p instanceof Guava)) return;
        GuavaForm pForm = (GuavaForm) p;
        //this.prepareStackFrame();
        this.launchForm(pForm.getID(), null, false);
    }

    public void prepareStackFrame() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int launchForm(UUID gid, String form, boolean bNew) {
        if (getLaunchType() == null)
            return JOptionPane.CANCEL_OPTION;

        Component p = this.getTopLevelAncestor();
        if (!(p instanceof Guava))
            return JOptionPane.CANCEL_OPTION;
        try {
            if (p instanceof GJFrame) {
                Constructor cons = s.getConstructor(new Class[]{UUID.class, UUID.class,
                        boolean.class, GSeed.class, java.awt.Frame.class, boolean.class});
                GJDialog o = (GJDialog) cons.newInstance(new Object[] { gid, ((GuavaForm) this.getTopLevelAncestor()).getID(),
                        bNew, ((GuavaForm) p).gs, p, true });

                o.setVisible(true);
                return o.dialogResult;
            } else {
                Constructor cons = s.getConstructor(new Class[]{UUID.class, UUID.class,
                        boolean.class, GSeed.class, java.awt.Dialog.class, boolean.class});
                GJDialog o = (GJDialog) cons.newInstance(new Object[] { gid, ((GuavaForm) this.getTopLevelAncestor()).getID(),
                        bNew, ((GuavaForm) p).gs, p, true });

                o.setVisible(true);
                return o.dialogResult;
            }
        } catch (Exception e) {
            System.err.println("ERROR: " + e.toString());
        }
        return 0;
    }


}
